//
//  SandTableView.m
//  Model
//
//  Created by YDJH on 2017/6/5.
//  Copyright © 2017年 YDJH. All rights reserved.
//

#import "SandTableView.h"
#import "HZSandTable.h"
#import "HZFurniture.h"
#import "UIImage+UIImageScale.h"
#import <objc/message.h>

@interface SandTableView ()

@property (nonatomic,copy)FurnitureClickBlock furnitureClickBlock;

@property (nonatomic,copy)HiddenFurnitureClickBlock hiddenfurnitureBlock;

@property (nonatomic, strong) NSMutableArray *pool; //重用池

@property (nonatomic,strong)HZSandTable *sandTable;

@property (nonatomic,assign)BOOL isFirst;

@property (nonatomic,strong)NSMutableArray <HiddenButton *> *hiddenBtnArray;

@end

@implementation SandTableView

- (NSMutableArray *)hiddenBtnArray {
    if (!_hiddenBtnArray) {
        _hiddenBtnArray = [NSMutableArray array];
    }
    return _hiddenBtnArray;
}

////刷新视图
- (void)reloadWithSandTable:(HZSandTable *)sandTable {
    
    self.sandTable = sandTable;
    [self initView];
    
    //重置家具视图
    [self resetSubviewsWithClass:[SandTableFurnitureView class]];
    __weak typeof(self) weakSelf = self;
    for (HZFurniture *furniture in sandTable.outsideFurniture) {
        SandTableFurnitureView *furnitureView = (SandTableFurnitureView *)[self goBackViewFromPoolWithClass:[SandTableFurnitureView class]];
        furnitureView.furniture = furniture;
        furnitureView.furnitureClickBlock = ^(HZFurniture *furniture, UIImageView *superView) {
            weakSelf.furnitureClickBlock(furniture, superView);
        };
        [self addSubview:furnitureView];
    }

}

- (instancetype)initWithFrame:(CGRect)frame furnitureClickBlock:(FurnitureClickBlock)furnitureClickBlock hiddenfurnitureBlock:(HiddenFurnitureClickBlock)hiddenfurnitureBlock{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.isFirst = YES;
        self.furnitureClickBlock = furnitureClickBlock;
        self.hiddenfurnitureBlock = hiddenfurnitureBlock;
        
    }
    return self;
}

- (void)initView {

    if (_isFirst) {
        
        self.image = [[UIImage imageNamed:_sandTable.picture] compressionImageToDataMaxFileSize:300];
        
        __weak typeof(self) weakSelf = self;
        for (HZHiddenFurniture *hiddenFurniture in self.sandTable.hiddenFurniture) {
            HiddenButton *button = [HiddenButton buttonWithType:UIButtonTypeCustom];
            button.hiddenFurniture = hiddenFurniture;
            if (self.sandTable.isHiddenFurniture) {
                button.hidden = NO;
            } else {
                button.hidden = YES;
            }
            button.hiddenfurnitureblock = ^(HZHiddenFurniture *hiddenFurniture) {
                weakSelf.hiddenfurnitureBlock(hiddenFurniture);
            };
            [self addSubview:button];
            [self.hiddenBtnArray addObject:button];
        }
        _isFirst = NO;
    }
    else {
        if (self.sandTable.isHiddenFurniture) {
            for (HiddenButton *button in self.hiddenBtnArray) {
                button.hidden = NO;
            }
        }else {
            for (HiddenButton *button in self.hiddenBtnArray) {
                button.hidden = YES;
            }
        }
    }
}

// 根据class返回未使用的view
- (UIView *)goBackViewFromPoolWithClass:(Class)class {
    
    __block UIView *view = nil;
    [self.pool enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:class] && !obj.haveuse) {
            *stop = YES;
            view = obj;
            view.haveuse = YES;
        }
        
    }];
    if (!view) {
        view = [[class alloc] init];
        view.haveuse = YES;
        [self addSubview:view];
        //新增加的添加到缓存池
        [self.pool addObject:view];
    }
    return view;
}
//将子视图的 haveuse 重置
- (void)resetSubviewsWithClass:(Class)class {
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:class]) {
            view.haveuse = NO;
            [view removeFromSuperview];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


@implementation UIView (Room_Property)

- (void)setHaveuse:(BOOL)haveuse {
    // 动态添加属性的本质是:让对象的某个属性与值产生关联
    objc_setAssociatedObject(self, @selector(haveuse), @(haveuse), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)haveuse {
    // 获取对应属性的值
    return [objc_getAssociatedObject(self, @selector(haveuse)) boolValue];
}

@end

